package cn.crce.cloud9.comp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * @author OnCloud9
 * @description
 * @project NRCE-TEST
 * @date 2022年09月13日 下午 09:45
 */
public class Case40 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Mouse Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MousePanel());
        frame.pack();
        frame.setVisible(true);
    }
}

//*********Found**********
class MousePanel extends JPanel {

    private JLabel b;

    public MousePanel() {
        b = new JLabel("  起始状态  ");
        setLayout(new BorderLayout());
        add(b, BorderLayout.SOUTH);

        addMouseMotionListener(new MouseMotion());
        setPreferredSize(new Dimension(300, 200));
    }

    //*********Found**********
    private class MouseMotion extends MouseAdapter {

        public void mouseMoved(MouseEvent e) {
            //*********Found**********
            b.setText(" 鼠标当前位置: " + e.getX() + " , " + e.getY());
        }
    }
}
